package com.javafly.sms.web.security;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.GrantedAuthority;

import com.javafly.sms.entity.security.Authority;
import com.javafly.sms.entity.security.Resource;
import com.javafly.sms.entity.security.Role;
import com.javafly.sms.entity.security.User;
import com.javafly.sms.service.security.SecurityEntityManager;
import com.javafly.sms.web.CrudActionSupport;

import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.hibernate.HibernateWebUtils;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.web.struts2.Struts2Utils;

/**
 * 角色管理Action.
 * 
 * 演示不分页的简单管理界面.
 * 
 * @author hym
 */
@Namespace("/security")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "resource.action", type = "redirect") })
public class ResourceAction extends CrudActionSupport<Resource> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private SecurityEntityManager securityEntityManager;

	//-- 页面属性 --//
	private Long id;
	private Long idt;
	public Long getIdt() {
		idt=120L;
		return idt;
	}
	public Long getId() {
		id=122L;
		return id;
	}
	private Resource entity;
	private Page<Resource> page = new Page<Resource>(13);//每页13条记录
	private List<Resource> allResourceList;//资源列表
	private List<Long> checkedAuthIds;//页面中钩选的权限id列表
	private String str;
	public Page<Resource> getPage() {
		return page;
	}
	public String getStr() {
		str="haha 哈哈";
		return str;
	}
     
	public void setStr(String str) {
		this.str = str;
	}

	//-- ModelDriven 与 Preparable函数 --//
	public Resource getModel() {
		return entity;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = securityEntityManager.getResource(id);
		} else {
			entity = new Resource();
		}
	}

	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
//		allResourceList = securityEntityManager.getAllResource();
//		return SUCCESS;
		
		List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = securityEntityManager.searchResource(page, filters);
//		GrantedAuthority[] tmp= SpringSecurityUtils.getCurrentUser().getAuthorities();
		
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		checkedAuthIds = entity.getAuthIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		//根据页面上的checkbox 整合Role的Authorities Set.
		HibernateWebUtils.mergeByCheckedIds(entity.getAuthorityList(), checkedAuthIds, Authority.class);
		//保存用户并放入成功信息.
		securityEntityManager.saveResource(entity);
		addActionMessage("保存资源成功");
						
		return list();
	}

	@Override
	public String delete() throws Exception {
		entity = securityEntityManager.getResource(id);
		if(entity.getAuthorityList()==null||entity.getAuthorityList().size()==0){
			securityEntityManager.deleteResource(id);
			addActionMessage("删除资源成功");
		}else{
			addActionMessage("删除资源失败，请先去除关联的权限！");
		}
		return list();
	}

	//-- 页面属性访问函数 --//
	/**
	 * list页面显示所有角色列表.
	 */
	public List<Resource> getAllResourceList() {
		return this.allResourceList;
	}

	/**
	 * input页面显示所有授权列表.
	 */
	public List<Authority> getAllAuthorityList() {
		return securityEntityManager.getAllAuthority();
	}

	/**
	 * input页面显示角色拥有的授权.
	 */
	public List<Long> getCheckedAuthIds() {
		return checkedAuthIds;
	}

	/**
	 * input页面提交角色拥有的授权.
	 */
	public void setCheckedAuthIds(List<Long> checkedAuthIds) {
		this.checkedAuthIds = checkedAuthIds;
	}
}